package chap13;//*******************************************************************
//  MagazineList.java       Author: Lewis/Loftus
//
//  Represents a collection of magazines.
//*******************************************************************

import java.util.ArrayList;

public class MagazineList
{
   private MagazineNode list;

   //----------------------------------------------------------------
   //  Sets up an initially empty list of magazines.
   //----------------------------------------------------------------
   public MagazineList()
   {
      list = null;
   }

   //----------------------------------------------------------------
   //  Creates a new MagazineNode object and adds it to the end of
   //  the linked list.
   //----------------------------------------------------------------
   public void add(Magazine mag)
   {
      MagazineNode node = new MagazineNode(mag);
      MagazineNode current;

      if (list == null)
         list = node;
      else
      {
         current = list;
         while (current.next != null)
            current = current.next;
         current.next = node;
      }
   }

   public void insert(int index , Magazine newMagazine)
   {
       int x = 0;
       MagazineNode node = new MagazineNode(newMagazine);
       MagazineNode current = list;

       if (list == null)
           list = node;
       else  if (index == 0)
       {
           node.next = current;
           list = node;
       }

            else {
           while (x < index - 1 && current.next != null) {
               current = current.next;
               x++;
           }
                node.next = current.next;
                current.next = node;

       }
   }

   public void delete(Magazine newMagazine)
   {
        MagazineNode node = new MagazineNode(newMagazine);
        MagazineNode current = list;
        MagazineNode prev = list;

        while (current.next != null) {
            current = current.next;
            if (current.magazine.toString().equals(node.magazine.toString())) {
                prev.next = current.next;
            } else {
                prev = current;
            }

        }
       if (!current.magazine.toString().equals(node.magazine.toString()))
           System.out.println("找不到节点，删除失败。");
   }


   //----------------------------------------------------------------
   //  Returns this list of magazines as a string.
   //----------------------------------------------------------------
   public String toString()
   {
      String result = "";

      MagazineNode current = list;

      while (current != null)
      {
         result += current.magazine + "\n";
         current = current.next;
      }

      return result;
   }

   //*****************************************************************
   //  An inner class that represents a node in the magazine list.
   //  The public variables are accessed by the MagazineList class.
   //*****************************************************************
   private class MagazineNode
   {
      public Magazine magazine;
      public MagazineNode next;

      //--------------------------------------------------------------
      //  Sets up the node
      //--------------------------------------------------------------
      public MagazineNode(Magazine mag)
      {
         magazine = mag;
         next = null;
      }
   }

   public void sort(){
       Magazine temp;
           if (list.magazine.compareTo(list.next.magazine) > 0){
               temp = list.next.magazine;
               list.next.magazine = list.magazine;
               list.magazine = temp;
           }
           MagazineNode x = list;
           MagazineNode y;

           while (x.next != null){
               y = x.next;
               while (y.next != null){
                   if (x.next.magazine.compareTo(y.next.magazine) > 0){
                       temp = y.next.magazine;
                       y.next.magazine = x.next.magazine;
                       x.next.magazine = temp;
                   }
                   y = y.next;
               }
               x = x.next;
           }

       }

   }


